package com.ziytek.web.citizen.util;


import com.ziytek.web.citizen.constant.NumConstant;
import com.ziytek.web.citizen.constant.ServerConstant;
import org.apache.commons.lang3.StringUtils;

import java.util.regex.Pattern;

/**
 * @author fenghx
 * @version v1.0
 * @className StringUtils
 * @description 功能描述
 * @date 2023/6/27 13:30
 **/
public class StringUtil {

    private static final Pattern PATTERN = Pattern.compile("[0-9]*");

    public static Integer stringToInt(String numStr, int len) {
        if (StringUtil.isEmpty(numStr)) {
            return NumConstant.INT_ZERO;
        }

        String[] intStr = numStr.split(ServerConstant.DATA_SEPARATOR);
        if (intStr.length != len) {
            return NumConstant.INT_ZERO;
        }

        if (NumConstant.INT_THREE == len) {
            String one = intStr[NumConstant.INT_ZERO];
            one = one.length() == NumConstant.INT_ONE ? NumConstant.INT_ZERO + one : one;
            String two = intStr[NumConstant.INT_ONE];
            two = two.length() == NumConstant.INT_ONE ? NumConstant.INT_ZERO + two : two;
            String three = intStr[NumConstant.INT_TWO];
            three = three.length() == NumConstant.INT_ONE ? NumConstant.INT_ZERO + three : three;
            numStr = one + two + three;
        }

        if (NumConstant.INT_TWO == len) {
            String one = intStr[NumConstant.INT_ZERO];
            one = one.length() == NumConstant.INT_ONE ? NumConstant.INT_ZERO + one : one;
            String two = intStr[NumConstant.INT_ONE];
            two = two.length() == NumConstant.INT_ONE ? NumConstant.INT_ZERO + two : two;
            numStr = one + two;
        }
        return isNumeric(numStr) ? Integer.parseInt(numStr) : NumConstant.INT_ZERO;
    }


    public static boolean isNumeric(String str) {
        try {
            if (StringUtil.isEmpty(str)) {
                return false;
            }
            str = str.replace(".", "");
            return PATTERN.matcher(str).matches();
        } catch (Exception e) {
            return false;
        }
    }

    public static Integer toInt(String numStr) {
        return isNumeric(numStr) ? Integer.parseInt(numStr) : NumConstant.INT_ZERO;
    }

    public static Float toFloat(String numStr) {
        return isNumeric(numStr) ? Float.parseFloat(numStr) : NumConstant.FLOAT_ZER;
    }

    public static Double toDouble(String numStr) {
        return isNumeric(numStr) ? Double.parseDouble(numStr) : NumConstant.DOUBLE_ZERO;
    }

    public static Long toLong(String numStr) {
        return isNumeric(numStr) ? Long.parseLong(numStr) : NumConstant.LONG_ZERO;
    }

    public static boolean isLong(String str) {
        try {
            if (StringUtil.isEmpty(str)) {
                return false;
            }
            var longValue =  Long.parseLong(str);
        } catch (Exception e) {
            return false;
        }

        return true;
    }

    public static boolean isEmpty(String str) {
        return StringUtils.isEmpty(str);
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static boolean noHasOneEmpty(Object... st) {
        return !hasOneEmpty(st);
    }

    public static boolean hasOneEmpty(Object... st) {
        boolean result = false;
        if (st == null) {
            return true;
        }
        for (Object temp : st) {
            if (temp == null) {
                return true;
            }
            if (isEmpty(String.valueOf(temp))) {
                result = true;
                break;
            }
        }
        return result;
    }
}
